﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Resources;

using G.Model;
using G.Runtime;

namespace Explorer
{
    public class MyGameSpace : Component, HasGameSpace
    {
        private Image _Image;
        private GameSpace _Game;
        
        private bool Get() {return false;}
        
        public MyGameSpace()
        {
            GameSpaceDef def = GameSpaceDef.Load(
                typeof(MyGameSpace).Assembly, "Game");
            _Game = new GameSpace(def);
            
            SetupImages();
            
            GameElement e = _Game.Elements[0];
            ClearArea(e.Location);
            e.LocationChanged += delegate(GameElement.LocationChangedEventArgs args)
            {
                ClearArea(args.newLocation);
            };
            
            _Game.Start();
        }

        void SetupImages()
        {
            ResourceManager rm = new ResourceManager(
                "Explore.r", Assembly.GetExecutingAssembly());

            _Image = (Image)rm.GetObject("cloud");
            Debug.Assert(_Image != null);
            
            _Image = _Image.GetThumbnailImage(29, 29, Get, IntPtr.Zero);

            for (int x = 0; x < _Game.Def.Width; x++) 
            {
                for (int y = 0; y < _Game.Def.Height; y++) 
                {
                    GameLocation loc = new GameLocation(x, y);
                    GameImage image = new GameImage(
                        _Image, loc);
                    _Game.Globals.TopImages.Add(image);
                }
            }
        }

        void ClearSquares()
        {
            foreach (GameElement element in _Game.Elements)
            {
                element.Icon = "";
            }
        }
        
        public GameSpace Game
        {
            get { return _Game; }
        }
        
        public void ClearArea(GameLocation center)
        {
            List<GameLocation> collection = new List<GameLocation>();
            Add(collection, center, -1, 0);
            Add(collection, center, 1, 0);
            Add(collection, center, 0, -1);
            Add(collection, center, 0, 1);
            Add(collection, center, -1, -1);
            Add(collection, center, 1, -1);
            Add(collection, center, -1, 1);
            Add(collection, center, 1, 1);
            Add(collection, center, 0, 0);
            
            foreach (GameLocation location in collection) 
            {
                if (_Game.Globals.TopImages.Contains(location))
                {
                    _Game.Globals.TopImages.Remove(location);
                }
            }
        }
        
        private void Add(
            List<GameLocation> list, GameLocation location,
            int dx, int dy)
        {
            int x = location.X + dx;
            int y = location.Y + dy;
            if (x >= 0 && x <= 5 && y >= 0 && y <= 5)
            {
                GameLocation loc = new GameLocation(x, y);
                list.Add(loc);
            }
        }
        
    }
}
